﻿using System;
using Microsoft.SPOT;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.SocketInterfaces;

namespace Gadgeteer.Modules.GHIElectronics
{
    // -- CHANGE FOR MICRO FRAMEWORK 4.2 --
    // If you want to use Serial, SPI, or DaisyLink (which includes GTI.SoftwareI2CBus), you must do a few more steps
    // since these have been moved to separate assemblies for NETMF 4.2 (to reduce the minimum memory footprint of Gadgeteer)
    // 1) add a reference to the assembly (named Gadgeteer.[interfacename])
    // 2) in GadgeteerHardware.xml, uncomment the lines under <Assemblies> so that end user apps using this module also add a reference.

    /// <summary>
    /// A LED7C module for Microsoft .NET Gadgeteer
    /// </summary>
    public class LED7C : GTM.Module
    {
		/// <summary>
		/// Represents possible display colors.
		/// </summary>
        public enum LEDColor
        {
            /// <summary>
            /// Red
            /// </summary>
            Red         = (1 << 2) | (0 << 1) | 0,
            
            /// <summary>
            /// Green
            /// </summary>
            Green       = (0 << 2) | (1 << 1) | 0,
            
            /// <summary>
            /// Blue
            /// </summary>
            Blue        = (0 << 2) | (0 << 1) | 1,
            
            /// <summary>
            /// Yellow
            /// </summary>
            Yellow      = (1 << 2) | (1 << 1) | 0,
            
            /// <summary>
            /// Cyan
            /// </summary>
            Cyan        = (0 << 2) | (1 << 1) | 1,
            
            /// <summary>
            /// Magenta
            /// </summary>
            Magenta     = (1 << 2) | (0 << 1) | 1,
            
            /// <summary>
            /// White
            /// </summary>
            White       = (1 << 2) | (1 << 1) | 1,

            /// <summary>
            /// Off
            /// </summary>
            Off  = 0,

        }

        private GTI.DigitalOutput m_RedPin;
        private GTI.DigitalOutput m_BluePin;
        private GTI.DigitalOutput m_GreenPin;

        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public LED7C(int socketNumber)
        {
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);

            socket.EnsureTypeIsSupported(new char[] { 'X', 'Y' }, this);

            m_RedPin = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Four, false, this);
            m_BluePin = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Three, false, this);
            m_GreenPin = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Five, false, this);
        }

        /// <summary>
        /// Sets the color of the module's LED to the passed in LEDColor enum.
        /// </summary>
        /// <param name="color"></param>
        public void SetColor(LEDColor color)
        {
            int r = ((int)(color) & 4);

            m_RedPin.Write((((int)color & 4) != 0 ? true : false));
            m_GreenPin.Write((((int)color & 2) != 0 ? true : false));
            m_BluePin.Write((((int)color & 1) != 0 ? true : false));
        }
    }
}
